#!/usr/bin/env python
# -*- coding:utf-8 -*- 
import urllib2
import re
import pickle
from find_qq_pic import*
def get_blog_url():
	lst_url=[]
	for i in xrange(1,13):
		url="http://www.designerhk.com/glossary?page=%i" % i
		print url
		print '----------------------------------'		
		body=urllib2.urlopen(url).read()
		li=re.findall('''<td class=\"views-field views-field-title active\">[\s\S]*?<a href=\"([\s\S]*?)\">[\s\S]*?</a>          </td>''',body)
		for i in li:
			i_url='http://www.designerhk.com%s' %i
			lst_url.append(i_url)
		print li
	f=open('www.designerhk.com.pic','w')
	pickle.dump(lst_url,f)
	f.close()

def find_sina_pic(url):
    print 'start'
    cj=cookielib.LWPCookieJar()
    opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    headers={
'User-Agent':'Mozilla/5.0 Firefox/3.6.8',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'en-us,en;q=0.5',
'Accept-Encoding':'deflate',
'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Keep-Alive':'115',
'Connection':'keep-alive',
'Cache-Control':'no-cache',}
    print 'start request url'
    req=urllib2.Request(url,headers=headers)
    html=opener.open(req).read()
    print len(html),'read html ok'
    title=re.findall('<title>([\s\S]*?)</title>',html)
    title=title[0]
    title=title.split('|')[0]
    #title=re.sub('\。（组图）_色娃_新浪博客','',title)
    lst_img_url=re.findall('<p><img alt=\"\" src=\"(/sites/designerhk.com/files/[\s\S]*?\" /></p>',html)
    #try:
     #   del lst_img_url[-1]
      #  del lst_img_url[-1]
    #except:
     #   pass
    lst_img=[]
    for i in lst_img_url:
	i_url='http://www.designerhk.com%s' %i
        lst_img.append(('','',i))
    return lst_img,title

if __name__=='__main__':
	#get_blog_url()
	f=open('www.designerhk.com.pic')
	lst_url=pickle.load(f)
	for i in lst_url:
		print i 
		lst_img,title=find_sina_pic(i)
		tag=u'设计'#raw_input('请输入标签：')
            	catalog=u'艺术'#raw_input('请输入目录：')
            	name=''#raw_input('请输入名称：')
		try:
            		data_site(lst_img,title,name,tag,catalog)
	    	except:
			pass
		print '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'
		print 'process %s work well' % i
